package jp.sourceforge.akjrcp.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer2;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeEvent;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeListener;
import jp.sourceforge.akjrcp.dropsomething2.DropFileGenericParameterSetting;
import jp.sourceforge.akjrcp.ui.param.ParamBoolean;
import jp.sourceforge.akjrcp.ui.param.ParamFile;
import jp.sourceforge.akjrcp.ui.param.ParamFile2;
import jp.sourceforge.akjrcp.ui.param.ParamItem;
import jp.sourceforge.akjrcp.ui.param.ParamItemActionListener;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public abstract class AbstractGenericParametersOptionEditor implements GenericParametersOptionEditor{
public List<GenericParametersEventListener> eventList=new ArrayList<GenericParametersEventListener>();
protected List<ParamItem> paramList=new ArrayList<ParamItem>();
ItemModifyCheck itemModifyCheck=new ItemModifyCheck();
public class ItemModifyCheck implements ParamItemActionListener{

	public void paramItemModified(ParamItem source) {
		boolean modified=false;
		for (int i = 0; i < paramList.size(); i++) {
			ParamItem item=paramList.get(i);
			if(item.isModified()){
				//log.info("modified:"+item.getKey());
				modified=true;
				break;
			}
			
		}
		
	
			fireModified(modified); 
		
	}
	
}


public void addParamList(ParamItem item){
	paramList.add(item);
	item.setParamItemActionListener(itemModifyCheck);
}

public void addListener(GenericParametersEventListener listener){
	eventList.add(listener);
}

public void removeListener(GenericParametersEventListener listener){
	eventList.remove(listener);
}
private void fireModified(boolean bool){
	GenericParametersEventListener[] listeners=null;
	synchronized(this){
		 listeners=eventList.toArray(new GenericParametersEventListener[eventList.size()]);
	}
	for (int i = 0; i < listeners.length; i++) {
		listeners[i].updateModified(bool);
	}

}
public abstract Composite createControls(Composite parent);

protected ParamBoolean samefolder,backup,avisynth;
protected ParamFile2 directoryChooser;

protected IPreferenceStore preferenceStore;

protected String preferenceKey;
protected Composite directoryInfoComposite;



public void createDirectoryInfo(Composite parent){
	
	directoryInfoComposite = new Composite(parent,SWT.NULL);
	GridData gdata=new GridData(GridData.FILL_HORIZONTAL);
	directoryInfoComposite.setLayoutData(gdata);
	GridLayout layout=new GridLayout(3,false);
	directoryInfoComposite.setLayout(LayoutBuilder.makeGridLayoutNoMargin(3,false));
	
	//check same folder
	samefolder = new ParamBoolean(directoryInfoComposite,SWT.CHECK,DropFileGenericParameterSetting.SAME_DIRECTORY,Messages.getString("textinsert.samefolder"),false);
	samefolder.getButton().addSelectionListener(new SelectionListener(){
		public void widgetSelected(SelectionEvent e) {
			sameFolderSelected();
		}
		public void widgetDefaultSelected(SelectionEvent e) {
		}
		
	});
	addParamList(samefolder);

	
	
	//directory choose
	directoryChooser = new ParamFile2(directoryInfoComposite,SWT.NULL,DropFileGenericParameterSetting.EXPORT_DIR,Messages.getString("textinsert.exportfolder"),FileDirChoicer2.DIR,false);
	
	
	directoryChooser.getFileDirChoicer().addFileListChangeListener(new FolderChoosed());
	directoryChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	addParamList(directoryChooser);
	//backup is maybe option
	backup = new ParamBoolean(directoryInfoComposite,SWT.CHECK,DropFileGenericParameterSetting.BACKUP,Messages.getString("textinsert.backup"),false);
	
	addParamList(backup);
	
	
	//default v
	sameFolderSelected();
	
	
	//if have preferenceStore restore from preferences
	//but somehow it conflict param setting
	if(preferenceStore!=null){
	preferenceStore.setDefault(getPreferenceKey()+".directoryinfo.samefolder", true);
	preferenceStore.setDefault(getPreferenceKey()+".directoryinfo.backup", true);
	preferenceStore.setDefault(getPreferenceKey()+".directoryinfo.directory", "");
	
	String directoryPath=preferenceStore.getString(getPreferenceKey()+".directoryinfo.directory");
	
	if(!new File(directoryPath).exists()){// exist path only.
		directoryPath="";
	}
	
	directoryChooser.setValue(directoryPath); //restore
	
	backup.setBoolean(preferenceStore.getBoolean(getPreferenceKey()+".directoryinfo.backup"));
	
	samefolder.setBoolean(preferenceStore.getBoolean(getPreferenceKey()+".directoryinfo.samefolder"));
	
	//restore default value. if value changed.
	SaveDirectoryInfoPreferenceStore ss=new SaveDirectoryInfoPreferenceStore();
	directoryChooser.getFileDirChoicer().addFileListChangeListener(ss);
	backup.getButton().addSelectionListener(ss);
	samefolder.getButton().addSelectionListener(ss);
	}
	
	
	
	
}


public class SaveDirectoryInfoPreferenceStore implements SelectionListener,FileStatusChangeListener{
	
	public void widgetDefaultSelected(SelectionEvent e) {
		
	}

	public void widgetSelected(SelectionEvent e) {
		Control control=(Control) e.getSource();
		if(control==backup){
			
			preferenceStore.setValue(getPreferenceKey()+".directoryinfo.backup", backup.getBoolean());
				
			}
		else if(control==samefolder){
			
			preferenceStore.setValue(getPreferenceKey()+".directoryinfo.samefolder", samefolder.getBoolean());
				
			}
	}

	public void addList(FileStatusChangeEvent event) {
		
		//if(event.getSource()==directoryChooser){ //some problem these code,i don know
		preferenceStore.setValue(getPreferenceKey()+".directoryinfo.directory", directoryChooser.getValue());
			//log.info(directoryChooser.getAbsoluteFilePath());
		//}
	}

	public void removeList(FileStatusChangeEvent event) {
		//if(event.getSource()==directoryChooser){
		preferenceStore.setValue(getPreferenceKey()+".directoryinfo.directory", directoryChooser.getValue());
			
		//}
	}
}

public boolean isBackup(){
	return backup.getBoolean();
}

public boolean isSameFolder(){
	return samefolder.getBoolean();
}

public String getChoosedPath(){
	return directoryChooser.getValue();
}

public void sameFolderSelected(){
	if(samefolder.getBoolean()){
		directoryChooser.setEnabled(false);
	}else{
		directoryChooser.setEnabled(true);
		
	}
	updateStatus();//should implement
}

public IPreferenceStore getPreferenceStore() {
	return preferenceStore;
}

public void setPreferenceStore(IPreferenceStore preferenceStore) {
	this.preferenceStore = preferenceStore;
}

public String getPreferenceKey() {
	return preferenceKey;
}

public void setPreferenceKey(String preferenceKey) {
	this.preferenceKey = preferenceKey;
}

public class FolderChoosed implements FileStatusChangeListener{

	public void removeList(FileStatusChangeEvent event) {
		updateStatus();
	}

	public void addList(FileStatusChangeEvent event) {
		updateStatus();
	}
	
}

public abstract void updateStatus();
}


